package com.example.management;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private Context mContext;
    private List<ListLikeResult.DataBean> mList;

    public RecyclerAdapter(Context context, List<ListLikeResult.DataBean> data) {
        this.mContext = context;
        this.mList = data;
    }

    public void setAdapterData(List<ListLikeResult.DataBean> data) {
        this.mList.clear();
        if (null != data) {
            this.mList.addAll(data);
        }
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_view, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        viewHolder.mId.setText("产品 id " +mList.get(position).getId() + "");
        viewHolder.mName.setText("产品 name " +mList.get(position).getName());
        viewHolder.mType.setText("产品 type " +mList.get(position).getType());
        viewHolder.mSupplierId.setText("产品 supplier_id " +mList.get(position).getSupplierId() + "");

    }

    @Override
    public int getItemCount() {
        return null != mList && !mList.isEmpty() ? mList.size() : 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mId;
        private TextView mName;
        private TextView mType;
        private TextView mSupplierId;

        public ViewHolder(View itemView) {
            super(itemView);
            mId = itemView.findViewById(R.id.id);
            mName = itemView.findViewById(R.id.name);
            mType = itemView.findViewById(R.id.type);
            mSupplierId = itemView.findViewById(R.id.supplier_id);
        }
    }
}
